package cn.aitstone.org.service.impl;

import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.service.impl.BaseServiceImpl;
import cn.aitstone.basic.utils.BaiduAuditUtils;
import cn.aitstone.basic.utils.ImgCensor;
import cn.aitstone.basic.utils.SendMailUtil;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.domain.Shop;
import cn.aitstone.org.domain.ShopAuditLog;
import cn.aitstone.org.mapper.EmployeeMapper;
import cn.aitstone.org.mapper.ShopAuditLogMapper;
import cn.aitstone.org.mapper.ShopMapper;
import cn.aitstone.org.service.IEmployeeService;
import cn.aitstone.org.service.IShopService;
import cn.aitstone.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sheldon
 * @since 2022-07-01
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    @Transactional
    public void settlement(Shop shop) {
        if (StringUtils.isEmpty(shop.getName()) &&
                StringUtils.isEmpty(shop.getTel()) &&
                StringUtils.isEmpty(shop.getAddress()) &&
                StringUtils.isEmpty(shop.getAdmin().getUsername()) &&
                StringUtils.isEmpty(shop.getAdmin().getPassword()) &&
                StringUtils.isEmpty(shop.getAdmin().getComfirmPassword()) &&
                StringUtils.isEmpty(shop.getAdmin().getPhone()) &&
                StringUtils.isEmpty(shop.getAdmin().getEmail())){
            throw new BusinessException("注册信息不能为空，请输入！");
        }
        // 判断店铺信息是否违规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        Boolean aBoolean1 = ImgCensor.ImgCensor(shop.getLogo());
        if (!aBoolean || !aBoolean1){
            throw new BusinessException("店铺信息涉嫌违规，请输入规范信息！");
        }

        Shop loadShop = shopMapper.loadByName(shop.getName());
        if(loadShop != null){
            throw new BusinessException("店铺已经被注册，请更换店铺信息！");
        }
        Employee admin = shop.getAdmin();
        Employee empTemp = employeeMapper.loadByUserName(admin.getUsername());
        if (empTemp != null && empTemp.getShopId() != null){
            throw new BusinessException("该管理员已经绑定了店铺，请重新输入！");
        }
        if (empTemp == null){
            employeeService.save(admin);
        }
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        admin.setShopId(shop.getId());
        // 修改管理员所属店铺的信息
        employeeMapper.update(admin);
    }

    /**
    * @Title: reject
    * @Description: 驳回
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/3 11:19
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(-1);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        SendMailUtil.sendMail("宠物乐园店铺入驻结果通知", "<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
                "店铺信息修改链接：<a href='http://localhost:8081/#/ShopRegisterRevise?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>", "2874863492@qq.com");
    }

    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(1);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        SendMailUtil.sendMail("宠物乐园店铺入驻结果通知", "<h1>亲，您的店铺入驻申请信息我平台已审核通过，请点击下方链接激活店铺</h1>"+
                "店铺激活链接：<a href='http://localhost:8080/shop/activate/"+shopAuditLog.getShopId()+"'>点我激活店铺</a>", "2874863492@qq.com");


    }

    /**
    * @Title: refuse
    * @Description: 拒绝
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/3 11:34
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    @Transactional
    public void refuse(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(-2);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        SendMailUtil.sendMail("宠物乐园店铺入驻结果通知", "<h1>亲，您的店铺入驻申请信息我平台已拒绝</h1>", "2874863492@qq.com");

    }

    @Override
    @Transactional
    public String activate(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(1);
        shopMapper.update(shop);
        return "您已成功激活";
    }

    @Override
    public Map<String, Object> loadShopEchartsData() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadShopEchartsData();
        List<String> states = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (ShopEchartVo shopEchartVo : shopEchartVos) {
            if (shopEchartVo.getState().equals(0)){
                states.add("待审核");
                counts.add(shopEchartVo.getCount());
            } else if (shopEchartVo.getState().equals(1)){
                states.add("已审核");
                counts.add(shopEchartVo.getCount());
            } else if (shopEchartVo.getState().equals(-1)){
                states.add("驳回");
                counts.add(shopEchartVo.getCount());
            } else if (shopEchartVo.getState().equals(2)){
                states.add("待激活");
                counts.add(shopEchartVo.getCount());
            }else if (shopEchartVo.getState().equals(-2)){
                states.add("拒绝");
                counts.add(shopEchartVo.getCount());
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x", states);
        map.put("y", counts);
        return map;
    }

}
